//index.js
//获取应用实例
var app = getApp();
//console.log(app.globalData);
Page({
  data: {
    userInfo: {},
    address: '',
    date: '',
    time:'',
    dining:{},
    shop_id:213,
    use_yie: 0,
    card_code: '',
    people:'',
    dining_table:'',
    shop_name:'',
    user_info:{},
    shop_info:{},
    shop_list_select:[],
    loading: 0,
    disabled: 0,
    order_amount:0,
    dining_table_list:[],
    numbers: ['1人', '2人', '3人', '4人', '5人', '6人', '7人', '8人', '9人','10人以上'],
  },
  //选择
  bindPickerChange: function (e) {
    var v = e.detail.value;
    if (e.currentTarget.dataset.type == "date") {
      this.setData({ date: v });
    }else if (e.currentTarget.dataset.type == "time") {
      this.setData({ time: v });
    } else if (e.currentTarget.dataset.type == "people") {
      this.setData({ people: this.data.numbers[v]});
    } else if (e.currentTarget.dataset.type == "dining_table"){
      this.setData({ dining: this.data.dining_table_list[v], dining_table: this.data.dining_table_list[v].name + "(" + this.data.dining_table_list[v].remark+")" });
      this.loadBookCart();

    } else if (e.currentTarget.dataset.type == "shop") {
      this.setData({ shop: this.data.shop_list[v], shop_name: this.data.shop_list[v].name, shop_id: this.data.shop_list[v].id  });
      this.get_dining_table();
    }
  },
//预约类型
  get_dining_table: function () {
    var that = this;
    app.api("mall/dining_table/get", {
      shop_id: this.data.shop_id,
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      var list = res.data.list;
      console.log("res", list);
      var dining_table_list_select = [];
      for (var i in list) {
        dining_table_list_select.push(list[i].name);
      }
      that.setData({ dining_table_list: list, dining_table_list_select: dining_table_list_select });

    })
  },
  //使用余额
  switch1Change: function (e) {
    if (this.data.user_info.store_member.money > this.data.data.total_price) {
      var use_yie = e.detail.value ? 1 : 0;
      this.setData({ use_yie: use_yie });
      this.setOrderAmount();
    } else {
      app.alert("余额不足，请先充值!");
      this.setData({ use_yie: 0 });
    }
  },

//计算金额
  loadBookCart: function (reload=0){
    if (reload == 1) {
      this.setData({
        loading: 0,
      });
    }
    var id = this.data.dining.id;
    var that = this;
    app.api("mall/dining_table_book/discounts", {
      shop_id: this.data.shop_id,
      card_code: this.data.card_code,
      diningTableId:id,
    }, function (res) {
      that.data.loading = 0;
      if (res.code != 200) {
        return;
      }
      if (res.data.cardRes && res.data.cardRes.code > 0) {
        app.alert(res.data.cardRes.msg);
        that.data.card_code = "";
      }
      that.setData({
        data: res.data
      });
      that.setOrderAmount();

    })
  },
//使用余额后的金额
  setOrderAmount: function () {
    if (!this.data.user_info.store_member || !this.data.data.total_price) {
      return;
    }
    var order_amount = 0;
    if (this.data.use_yie == 1) {
      order_amount = this.data.user_info.store_member.money > this.data.data.total_price ? 0 : (this.data.data.total_price - this.data.user_info.store_member.money);
    } else {
      order_amount = this.data.data.total_price;
    }
    order_amount.toFixed(2)
    this.setData({ order_amount: order_amount })
  },
//分店列表
  get_shop_list: function () {
    var that = this;
    app.api("mall/shop/get", {
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      var list = res.data.list;
      console.log("res", list);
      var shop_list_select=[];
      for (var i in list){
        shop_list_select.push(list[i].name);
      }
      that.setData({ shop_list: list, shop_list_select: shop_list_select});

    })
  },

  get_shop: function () {
    var that = this;
    app.api("mall/shop/find", {
      id: this.data.shop_id
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      } 
      that.setData({ shop_info: res.data });
    })
  },
  //用户信息
  getInfo: function () {
    var that = this;
    app.api("api/user/info", {
      fields: "avatarUrl,user_id,last_update_time,nickname,phone"
    }, function (res) {

      if (res.code != 200) {
       // app.alert(res.msg);
        return;
      }

      that.setData({ user_info: res.data });
    })
  },

onShow: function () {
    var reload = app.getPageData("checkout/index",'reload');
    var card_code = app.getPageData("card/index", 'card_code');
    if (reload==1){
      app.setPageData("checkout/index", { reload:0});
      this.loadBookCart(1);
    }
    if (card_code ){
      app.setPageData("card/index", { card_code: 0, card_id: 0 });
      this.data.card_code = card_code;
      this.loadBookCart(1);
    }

  },
  
  
  onLoad: function (options) {
    this.setData({ extConfig: app.getExtConfigSync() });
    var shop_info = app.getShopInfo();
    this.data.shop_id = shop_info.id ;
    if (options.scene) {
      this.data.shop_id = decodeURIComponent(options.scene);
    }

    this.get_dining_table();
    this.getInfo();
    this.get_shop();

  },
  //打开商家所在地点
  openlocation: function () {
    wx.openLocation({
      latitude: Number(this.data.shop_info.latitude),
      longitude: Number(this.data.shop_info.longitude),
      scale: 28,
      address: this.data.shop_info.fulladdress,
      name: this.data.shop_info.name,
    })
  },

  //提交 
  formSubmit: function (e) {
    var formId = e.detail.formId;
    var postData = e.detail.value;
    if (this.data.loading == 1) {
      return;
    }

    this.setData({ 'loading': 1 });
    this.setData({ 'disabled': 1 });
    var that = this;
    var title = postData.title;
    //提交预约
    app.api("mall/dining_table_book/add", {
      date: this.data.date,
      book_time: this.data.time,
      dining_table_name: this.data.dining_table,
      remark: postData.remark,
      phone: postData.phone,
      contacts: postData.contacts,
      people: this.data.people,
      shop_id: this.data.shop_id,
      dining_table_id: this.data.dining.id,
      money: this.data.dining.money,
      use_yie: this.data.use_yie,
      card_code: this.data.card_code,
      method: 'POST',
    }, function (res) {
      that.setData({ 'loading': 0 });
      that.setData({ 'disabled': 0 });
      var title = "";
      if (res.code != 200) {
        app.alert(res.msg);
        return ;
      } 
      var order_sn = res.data.order_sn;
      app.api("api/pay/topay", {
        order_sn: order_sn
      }, function (res) {
        if (res.code != 200) {
          app.alert(res.msg);
          that.data.payloading = 0;
          that.setData({ 'loading': 0 });
          that.setData({ 'disabled': 0 });
          return false;
        }
        if (res.data.over == 1) {
          that.suc();
          return;
        }
        var params = res.data;
        //params.total_fee = 0.1;
        params.success = function (res) {
          that.setData({ loading: 0, disabled: 0 });
          that.suc();
        }
        params.fail = function (res) {
          console.log(res);
          if (res.errMsg == 'requestPayment:fail cancel') {
            that.setData({ loading: 0, disabled: 0 });
            return false;
          }
          app.alert(res.errMsg);
        }
        console.log(params);
        wx.requestPayment(params)
      })
    })
  },



  suc: function () {
    var _this = this;
    wx.showToast({
      title: '预约成功',
      icon: 'success',
      duration: 2000
    });
    wx.navigateTo({
      url: '/book/pages/order',
    })
  },
  navigateBack:function(){
    wx.navigateBack();
  }


})
